package com.stu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stu.handlers.CommonExecption;
import com.stu.mapper.ChannelMapper;
import com.stu.mapper.ClazzMapper;
import com.stu.mapper.StudentDescMapper;
import com.stu.pojo.Channel;
import com.stu.pojo.Clazz;
import com.stu.pojo.Student;
import com.stu.mapper.StudentMapper;
import com.stu.pojo.StudentDesc;
import com.stu.pojo.vo.StudentDescVo;
import com.stu.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2023-12-20
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    StudentDescMapper studentDescMapper;
    @Autowired
    ClazzMapper clazzMapper;
    @Autowired
    ChannelMapper channelMapper;

    @Override
    public StudentDescVo getByIdWithDesc(Long id) {
        StudentDescVo studentDescVo = new StudentDescVo();
        //通过id查询student基础信息
        Student student = studentMapper.selectById(id);
        BeanUtils.copyProperties(student,studentDescVo);
        //获取学号
        Long sid = student.getSid();
        //通过学号获取desc信息
        LambdaQueryWrapper<StudentDesc> qw = new LambdaQueryWrapper<>();
        qw.eq(StudentDesc::getSid,sid);
        StudentDesc studentDesc = studentDescMapper.selectOne(qw);
        BeanUtils.copyProperties(studentDesc,studentDescVo);
        return studentDescVo;
    }

    @Override
    public boolean updateByIdWithDesc(StudentDescVo studentDescVo) {
        //查询班级的id
        LambdaQueryWrapper<Clazz> qw = new LambdaQueryWrapper<>();
        qw.eq(Clazz::getName,studentDescVo.getClazzName());
        Clazz clazz = clazzMapper.selectOne(qw);
        Long clazzId;
        if(Objects.isNull(clazz)){
            throw new CommonExecption("请选择有效班级信息");
        }else {
            clazzId = clazz.getId();
        }
        //查询渠道的id
        LambdaQueryWrapper<Channel> cqw = new LambdaQueryWrapper<>();
        cqw.eq(Channel::getName,studentDescVo.getChannelName());
        Long channelId = channelMapper.selectOne(cqw).getId();
        studentDescVo.setClazzId(clazzId);
        studentDescVo.setChannelId(channelId);
        StudentDesc studentDesc = new StudentDesc();
        studentDesc.setSid(studentDescVo.getSid());
        studentMapper.updateById(studentDescVo);
        BeanUtils.copyProperties(studentDescVo,studentDesc);
        LambdaQueryWrapper<StudentDesc> sqw = new LambdaQueryWrapper<>();
        sqw.eq(StudentDesc::getSid,studentDescVo.getSid());
        studentDescMapper.update(studentDesc,sqw);
        return true;
    }

    @Override
    public List<StudentDescVo> getStudentAndDesc(String name, String clazzName,String study,String school,String payStatus) {
        return studentMapper.getStudentAndDesc(name,clazzName,study,school,payStatus);
    }

    @Override
    public Page<StudentDescVo> lendingList(Integer page, Integer pageSize, String name, String clazzName,String study,String school,String payStatus) {
        Page<StudentDescVo> studentDescVoPage = new Page<>(page, pageSize);
        studentMapper.lendingList(studentDescVoPage,name,clazzName,study,school,payStatus);
        return studentDescVoPage;
    }
}
